package ctn.sdk.youdao.service;

import cnt.sdk.youdao.model.Translation;

import com.google.gson.Gson;

import ctn.sdk.youdao.until.HttpUtil;

/**
 * Created by Zale on 4/2/14.
 */
public class TranslateService extends BaseService{

    public Translation translate(String query) throws Exception {
    	StringBuffer sb = new StringBuffer();
    	sb.append("keyfrom=").append(getKeyFrom());
    	sb.append("&key=").append(getKey());
    	sb.append("&type=data");
    	sb.append("&doctype=json");
    	sb.append("&version=1.1");
    	sb.append("&q=").append(query);
    	String responseJSON = HttpUtil.httpGet(getBaseURL(), sb.toString());
    	Gson gson = new Gson();
    	Translation t = gson.fromJson(responseJSON, Translation.class);
    	return t;

    }
 public static void main(String ... args) throws Exception {
//     HttpClient client = new HttpClient();
//     String url = "http://fanyi.youdao.com/openapi.do?keyfrom=cuotoniu&key=491659682&type=data&doctype=json&version=1.1&q=翻";
//     URI uri = new URI(url,false,"UTF-8");
//     GetMethod httpget = new GetMethod(uri.toString());
//     client.executeMethod(httpget);
//     System.out.println(httpget.getResponseBodyAsString());
	 TranslateService service = new TranslateService();
	 Translation t = service.translate("translation");
	 System.out.println(t.getTranslation());
 }

}



